﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Animation;

namespace Learn001.Controls
{
    public class Desktop : Canvas
    {
        private string clickName = string.Empty;//记录单击控件名称
        private List<double> Tops = new List<double>();//Top位置坐标
        private List<CustomPanel> Panels = new List<CustomPanel>();//Panel集
        private int ctlNum = int.MaxValue;
        private int rowCtlNum = int.MinValue;
        private bool isLoaded = false;

        public static readonly DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int), typeof(Desktop), new PropertyMetadata(1, RowsChanged));

        public int Rows
        {
            get { return (int)GetValue(RowsProperty); }
            set { SetValue(RowsProperty, value); }
        }

        private static void RowsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Desktop desktop = sender as Desktop;
            if (desktop == null) return;

            int rows = 0;
            if (e.NewValue != null)
                int.TryParse(e.NewValue.ToString(), out rows);

            if (rows > 0)
                desktop.rowCtlNum = (desktop.ctlNum / rows) + (desktop.ctlNum % rows > 0 ? 1 : 0);
        }

        public Desktop()
            : base()
        {
            this.MinWidth = 480;
            this.MinHeight = 400;

            this.Loaded += new RoutedEventHandler(Desktop_Loaded);
        }

        void Desktop_Loaded(object sender, RoutedEventArgs e)
        {
            if (isLoaded) return;
            for (int i = 0; i < this.Children.Count; i++)
            {
                CustomPanel panel = this.Children[i] as CustomPanel;
                if (panel == null) continue;

                if (panel.Name == null || panel.Name.Trim() == "")
                    panel.Name = "panel" + i.ToString();

                panel.Click += BodyPanel_Click;
                panel.Margin = new Thickness(5, 3, 3, 3);
                Panels.Add(panel);
            }

            ctlNum = Panels.Count;
            if (Rows == 0)
                Rows = 1;
            rowCtlNum = (ctlNum / Rows) + (ctlNum % Rows > 0 ? 1 : 0);

            this.SizeChanged += new SizeChangedEventHandler(Desktop_SizeChanged);
            Desktop_SizeChanged(sender, null);
            isLoaded = true;
        }

        private void Desktop_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Double LWidth = 0;//小对象的宽度            
            Double MWidth = 0;//大对象的宽度
            Double MHeight = 0;//大对象的高度
            Double LHeight = 0;//小对象的高度

            CalcPanelWidthAndHeight(out LWidth, out LHeight, out MWidth, out MHeight);

            //判断执行动画
            if (string.IsNullOrEmpty(clickName) == true)//初始化动画,也就是第一次点击
            {
                this.Children.Clear();
                SetNormalState(MHeight, null);
            }
            else
            {
                Initialization(MWidth, MHeight, LWidth, LHeight);
            }
        }

        private void SetNormalState(double height, Storyboard sb)
        {
            Double MiddleWidth = this.ActualWidth / rowCtlNum - 5;//对象宽度
            Double MiddleHeight = height / Rows - rowCtlNum - 5;//对象高度
            for (int i = 0; i < ctlNum; i++)
            {
                CustomPanel temp = Panels[i];

                double left = 0;
                double top = 0;
                if (i < rowCtlNum)//控制行Top
                {
                    top = 2;//第一行顶点
                    left = MiddleWidth * i + i * 5;//第一行的左边距
                }
                else
                {
                    top = this.ActualHeight / Rows;//第二行顶点
                    left = MiddleWidth * (i - rowCtlNum) + (i - rowCtlNum) * 5;//第二行的左边距
                }

                if (sb == null)
                {
                    temp.Width = MiddleWidth;
                    temp.Height = MiddleHeight;

                    temp.SetValue(Canvas.LeftProperty, left);//与Margin有所不同
                    temp.SetValue(Canvas.TopProperty, top);
                    this.Children.Add(temp);
                }
                else
                    ReducePanel(sb, temp, MiddleWidth, MiddleHeight, top, left);
            }
        }

        private void CalcPanelWidthAndHeight(out double lWidth, out double lHeight, out double mWidth, out double mHeight)
        {
            lWidth = 200;//小对象的宽度            
            mWidth = this.ActualWidth - lWidth - 5;//大对象的宽度
            mHeight = this.ActualHeight - 5;//大对象的高度
            lHeight = (mHeight - (ctlNum - 1) * 6) / (ctlNum - 1);//小对象的高度
        }

        private void BodyPanel_Click(object sender, RoutedEventArgs e)
        {
            CustomPanel panel = sender as CustomPanel;
            //Canvas ca = panel.Parent as Canvas;

            Double LWidth = 0;//小对象的宽度            
            Double MWidth = 0;//大对象的宽度
            Double MHeight = 0;//大对象的高度
            Double LHeight = 0;//小对象的高度

            CalcPanelWidthAndHeight(out LWidth, out LHeight, out MWidth, out MHeight);

            //判断执行动画
            if (string.IsNullOrEmpty(clickName) == true)//初始化动画,也就是第一次点击
            {
                clickName = panel.Name;//保存为上一个点击按钮
                Initialization(MWidth, MHeight, LWidth, LHeight);
            }
            else
            {
                Storyboard sb = new Storyboard();
                if (panel.Name == clickName)//还原界面
                {
                    //当点击上一个点击的按钮时，还原初始界面
                    SetNormalState(MHeight, sb);
                    clickName = string.Empty;
                }
                else//位置交换
                {
                    CustomPanel ctl = this.FindName(clickName) as CustomPanel;
                    MaxPanel(sb, panel, MWidth - 5, MHeight - 10);
                    ctl.Button.IsMax = false;
                    ReducePanel(sb, ctl, LWidth, LHeight, Canvas.GetTop(panel), MWidth);
                    clickName = panel.Name;
                }
                sb.Begin();
            }
        }

        /// <summary>
        /// 获取每个对象顶点的位置数组
        /// </summary>
        /// <param name="ca"></param>
        /// <returns></returns>
        public List<double> topList(Canvas ca)
        {
            List<double> ListTmp = new List<double>();//Top位置坐标
            //初始化Tops坐标
            //Tops.Add((ctlNum - 1));           
            Double MHeight = this.ActualHeight - 5;//大对象的高度
            Double LTop = (MHeight - (ctlNum - 1) * 6) / (ctlNum - 1);//小对象的高度
            for (int i = 0; i < ctlNum - 1; i++)
            {
                //95：初始化后控件高 3：间距
                ListTmp.Add(i * LTop + i * 5 + 5);
            }
            return ListTmp;
        }

        /// <summary>
        /// 所有对象归位
        /// </summary>
        /// <param name="MaxWidth">大对象的宽</param>
        /// <param name="MaxHeight">大对象的高</param>
        /// <param name="LWidth">小对象的宽</param>
        /// <param name="LHeight">小对象的高</param>
        private void Initialization(double MaxWidth, double MaxHeight, Double LWidth, Double LHeight)
        {
            Tops = topList(this);
            Storyboard sb = new Storyboard();
            int iTop = 0;
            for (int i = 0; i < ctlNum; i++)
            {
                CustomPanel ctl = Panels[i];
                if (ctl.Name == clickName)//判断是否是单击对象
                {
                    MaxPanel(sb, ctl, MaxWidth - 5, MaxHeight - 10);//第一次点击时
                }
                else
                {
                    ctl.Button.IsMax = false;
                    ReducePanel(sb, ctl, LWidth, LHeight, Tops[iTop], MaxWidth);//其它的缩小归位
                    iTop++;
                }
            }
            sb.Begin();
        }

        /// <summary>
        /// 放大Panel对象
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="panel"></param>
        private void MaxPanel(Storyboard sb, CustomPanel panel, double MaxWidth, double MaxHeight)
        {
            sb.Children.Add(CreateDoubleAnimation(panel, "(FrameworkElement.Width)", panel.Width, MaxWidth));
            sb.Children.Add(CreateDoubleAnimation(panel, "(FrameworkElement.Height)", panel.Height, MaxHeight));
            sb.Children.Add(CreateDoubleAnimation(panel, "(Canvas.Top)", Canvas.GetTop(panel), 5));
            sb.Children.Add(CreateDoubleAnimation(panel, "(Canvas.Left)", Canvas.GetLeft(panel), 0));
        }

        /// <summary>
        /// 缩小Panel对象,也就是小窗口的大小
        /// </summary>
        /// <param name="sb">动画板</param>
        /// <param name="panel">动画对象</param>
        /// <param name="width">宽度（缩小目标）</param>
        /// <param name="height">高度（缩小目标）</param>
        /// <param name="Top">对象最终顶边</param>
        /// <param name="Left">对象最终左边</param>
        private void ReducePanel(Storyboard sb, CustomPanel panel, double width, double height, double Top, double Left)
        {
            sb.Children.Add(CreateDoubleAnimation(panel, "(FrameworkElement.Width)", panel.Width, width));
            sb.Children.Add(CreateDoubleAnimation(panel, "(FrameworkElement.Height)", panel.Height, height));
            sb.Children.Add(CreateDoubleAnimation(panel, "(Canvas.Top)", Canvas.GetTop(panel), Top));
            sb.Children.Add(CreateDoubleAnimation(panel, "(Canvas.Left)", Canvas.GetLeft(panel), Left));
        }

        /// <summary>
        /// 创建一个DoubleAnimation动画对象
        /// </summary>
        /// <param name="element">动画对象</param>
        /// <param name="property">动画对象属性</param>
        /// <param name="from">起始值</param>
        /// <param name="to">结束值</param>       
        /// <returns></returns>
        public static DoubleAnimation CreateDoubleAnimation(DependencyObject element, string property, double from, double to)
        {
            DoubleAnimation da = new DoubleAnimation();
            TimeSpan tsBeginTime = new TimeSpan(0, 0, 0, 0, 0);//开始时间
            TimeSpan tsDuration = TimeSpan.FromMilliseconds(1000);//播放时间的长度
            da.BeginTime = tsBeginTime;
            da.Duration = new Duration(tsDuration);//播放长度
            da.To = to;
            da.From = from;
            da.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 3 };
            Storyboard.SetTarget(da, element);
            Storyboard.SetTargetProperty(da, new PropertyPath(property));
            return da;
        }
    }
}
